using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace net.notdot.Attercop.Client
{
    public partial class BrowseWindow : Form
    {
        public BrowseWindow(IEnumerable<PathNode> shares)
        {
            InitializeComponent();

            foreach (PathNode share in shares)
            {
                if (share.IsDir)
                {
                    tvMain.Nodes.Add(BuildTree(share));
                }
            }
        }

        private TreeNode BuildTree(PathNode share)
        {
            TreeNode ret = new TreeNode(share.Name);
            ret.ImageIndex = 0;
            ret.Tag = share;

            foreach (PathNode child in share.Children)
            {
                if (child.IsDir)
                {
                    ret.Nodes.Add(BuildTree(child));
                }
            }

            return ret;
        }

        private void BrowseWindow_Load(object sender, EventArgs e)
        {
            ImageList iml = ((MainWindow)MdiParent).imlSmallFileIcons;
            tvMain.ImageList = iml;
            lvMain.SmallImageList = iml;
        }

        private void tvMain_AfterSelect(object sender, TreeViewEventArgs e)
        {
            lvMain.Items.Clear();
            PathNode current = (PathNode)tvMain.SelectedNode.Tag;

            foreach (PathNode child in current.Children)
            {
                if (!child.IsDir)
                {
                    MainWindow.Instance.AddFileIcon(child.Extension);
                    lvMain.Items.Add(new ListViewItem(new string[] {
                        child.Name,
                        child.Extension.Substring(1),
                        child.TotalSizeHuman
                    }, child.Extension));
                }
            }
        }
    }
}